import { tokenKey, host, loginUrl } from "../config";
var params = {}
export const http = (url, method, data, header = {}, dataType = 'json', needToast) => {
  console.log(host, url)
  !needToast && tt.showLoading({
    title: '加载中'
  })
  try {
    const token = tt.getStorageSync(tokenKey);
    console.log(token)
    if (header) {
      header.Authorization = `Bearer ${token}`
    }
    return new Promise((resolve, reject) => {
      tt.request({
        url: host + url,
        method,
        data,
        dataType,
        header,
        success: res => {
          if (res.statusCode === 403) {
            console.log('success 403')
            params.url = url;
            params.method = method;
            params.data = data;
            params.header = header;
            params.dataType = dataType;
            tt.login({
              success: res => {
                console.log('login success')
                tt.request({
                  url: host + loginUrl,
                  method: 'GET',
                  data: {
                    code:res.code
                  },
                  success: async res => {
                    console.log('server login success')
                    if (res.statusCode === 200) {
                      if (res.data.code === 200) {
                        if (params.url) {
                          tt.setStorageSync(tokenKey, res.data.data.token);
                          const result = await http(params.url, params.method, params.data, params.header, params.dataType);
                          tt.hideLoading();
                          params = {};
                          resolve(result);
                        } else {
                          resolve(res.data.data);
                        }
                      } else {
                        !needToast && tt.hideLoading();
                        !needToast && tt.showToast({
                          title: '未知错误！',
                          duration: 3000,
                          icon: 'error',
                          mask: false,
                        });
                        resolve(null);
                      }
                    }
                  }
                })
              },
              fail: () =>{
                !needToast && tt.hideLoading();
                !needToast && tt.showToast({
                  title: '飞书异常！',
                  duration: 3000,
                  icon: 'error',
                  mask: false,
                });
                resolve(null);
              }
            })
          } else if (res.statusCode === 200) {
            tt.hideLoading();
            if (res.data.code === 200) {
              resolve(res.data.data);
            } else {
              !needToast && tt.showToast({
                title: '未知错误！',
                duration: 3000,
                icon: 'error',
                mask: false,
              });
              resolve(null);
            }
          }
          console.log(res)
        },
        fail:res => {
          !needToast && tt.hideLoading();
          !needToast && tt.showToast({
            "title": "服务异常",
            "duration": 3000,
            "icon": "error",
            "mask": false,
            success(res) {
              console.log(JSON.stringify(res));
            },
            fail(res) {
              console.log(`showToast fail: ${JSON.stringify(res)}`);
            }
          });
          console.log('fail', JSON.stringify(res));
          reject(res);
        }
      })
    })
  } catch (error) {
      tt.hideLoading();
      console.log(`setStorageSync fail: ${JSON.stringify(error)}`);
      return Promise.reject(error)
  }
}